package i.i.myapplication.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import i.i.myapplication.R;

//package i.i.myapplication.view;

import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.database.ExoDatabaseProvider;
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DefaultDataSource;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;

import java.io.File;

import i.i.myapplication.R;
import i.i.myapplication.tools.CacheManager;

//package i.i.myapplication.view;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.upstream.DefaultDataSource;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;

import java.io.File;

import i.i.myapplication.R;
import i.i.myapplication.tools.CacheManager;

public class VideoDetailActivity extends AppCompatActivity {

    private PlayerView playerView;
    private ExoPlayer player;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_detail);

        playerView = findViewById(R.id.player_view);

        Intent intent = getIntent();
        String videoPath = intent.getStringExtra("video_path");
        int videoResId = intent.getIntExtra("video_res_id", -1);
        Uri videoUri = null;

        if (videoPath != null && !videoPath.isEmpty()) {
            videoUri = Uri.fromFile(new File(videoPath));
        } else if (videoResId != -1) {
            videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + videoResId);
        } else {
            finish();
            return;
        }

        Context context = getApplicationContext();
        DefaultDataSource.Factory upstreamFactory = new DefaultDataSource.Factory(context);
        SimpleCache cache = CacheManager.getInstance(context);

        CacheDataSource.Factory cacheDataSourceFactory = new CacheDataSource.Factory()
                .setCache(cache)
                .setUpstreamDataSourceFactory(upstreamFactory)
                .setFlags(CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR);

        player = new ExoPlayer.Builder(this)
                .setMediaSourceFactory(new DefaultMediaSourceFactory(cacheDataSourceFactory))
                .build();

        playerView.setPlayer(player);
        MediaItem mediaItem = MediaItem.fromUri(videoUri);
        player.setMediaItem(mediaItem);
        player.prepare();
        player.play();
    }
    @Override
    protected void onStop() {
        super.onStop();
        if (player != null) {
            player.release();  // 释放播放器资源
            player = null;
        }
    }


}
